{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.创建ndarray对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "n1: \n",
      " [ 1.  2.  3.]\n",
      "\n",
      "n2: \n",
      " [[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "n3: \n",
      " [b'a' b'b' b'c']\n"
     ]
    }
   ],
   "source": [
    "n1 = np.array([1.0, 2.0, 3.0], dtype=np.float32)\n",
    "n2 = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int32)\n",
    "n3 = np.array(['a', 'b', 'c'], dtype='|S1')\n",
    "\n",
    "print(\"\\nn1: \\n\", n1)\n",
    "print(\"\\nn2: \\n\", n2)\n",
    "print(\"\\nn3: \\n\", n3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.访问ndarray对象的type、shape、ndim、size、dtype、itemsize和data属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "type:  <class 'numpy.ndarray'>\n",
      "shape:  (3,)\n",
      "ndim:  1\n",
      "size:  3\n",
      "dtype:  |S1\n",
      "itemsize:  4\n",
      "data:  <memory at 0x000001EF7E898708>\n"
     ]
    }
   ],
   "source": [
    "print(\"type: \", type(n1))\n",
    "print(\"shape: \", n1.shape)\n",
    "print(\"ndim: \", n1.ndim)\n",
    "print(\"size: \", n1.size)\n",
    "print(\"dtype: \", n3.dtype)\n",
    "print(\"itemsize: \", n1.itemsize)\n",
    "print(\"data: \", n1.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.创建数组:列表/元组/列表-元组混合方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "v1: \n",
      " [[ 1.  2.  3.]\n",
      " [ 4.  5.  6.]]\n",
      "\n",
      "v2: \n",
      " [[ 1.  2.  3.]\n",
      " [ 4.  5.  6.]]\n",
      "\n",
      "v3: \n",
      " [[ 1.  2.  3.]\n",
      " [ 4.  5.  6.]]\n"
     ]
    }
   ],
   "source": [
    "# 以列表的形式创建ndarray对象\n",
    "v1 = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
    "print(\"\\nv1: \\n\", v1)\n",
    "\n",
    "# 以元组的形式创建ndarray对象\n",
    "v2 = np.array(((1.0, 2.0, 3.0), (4.0, 5.0, 6.0)))\n",
    "print(\"\\nv2: \\n\", v2)\n",
    "\n",
    "# 以列表和元组混合形式创建ndarray对象\n",
    "v3 = np.array([(1.0, 2.0, 3.0), [4.0, 5.0, 6.0]])\n",
    "print(\"\\nv3: \\n\", v3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4.自带的数组创建方法\n",
    "\n",
    "- np.zeros()\n",
    "- np.ones()\n",
    "- np.arange()\n",
    "- np.arange().reshape()\n",
    "- np.linspace()\n",
    "- np.random.random()\n",
    "- np.random.randint()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v1: \n",
      " [[ 0.  0.  0.]\n",
      " [ 0.  0.  0.]]\n",
      "\n",
      "v2: \n",
      " [[ 1.  1.  1.]\n",
      " [ 1.  1.  1.]]\n",
      "\n",
      "v3: \n",
      " [0 2 4 6 8]\n",
      "v4: \n",
      " [ 0.   2.5  5.   7.5]\n",
      "v5: \n",
      " [[ 0.   2.5]\n",
      " [ 5.   7.5]]\n",
      "v6: \n",
      " [ 0.     0.375  0.75   1.125  1.5  ]\n",
      "\n",
      "v7: \n",
      " [[ 0.69157966  0.63216217  0.24791072]\n",
      " [ 0.80385918  0.72684532  0.3107819 ]]\n",
      "\n",
      "v8: \n",
      " [[1 3 4 3]\n",
      " [1 1 2 3]\n",
      " [1 2 3 3]]\n"
     ]
    }
   ],
   "source": [
    "v1 = np.zeros((2, 3))\n",
    "print(\"v1: \\n\", v1)\n",
    "\n",
    "v2 = np.ones((2, 3))\n",
    "print(\"\\nv2: \\n\", v2)\n",
    "\n",
    "v3 = np.arange(0, 10, 2)\n",
    "v4 = np.arange(0, 10, 2.5)\n",
    "v5 = np.arange(0, 10, 2.5).reshape(2, 2)\n",
    "print(\"\\nv3: \\n\", v3)\n",
    "print(\"v4: \\n\", v4)\n",
    "print(\"v5: \\n\", v5)\n",
    "\n",
    "v6 = np.linspace(0, 1.5, 5)\n",
    "print(\"v6: \\n\", v6)\n",
    "\n",
    "v7 = np.random.random((2, 3))\n",
    "print(\"\\nv7: \\n\", v7)\n",
    "\n",
    "v8 = np.random.randint(1, 5, size=(3, 4))\n",
    "print(\"\\nv8: \\n\", v8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
